package utilities;

import com.googlecode.javacpp.Loader;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_core.*;

/**
 *
 * @author Bhagya
 */
public class FindContours {

    double largest, result1 = 0;
    // double secondLargest;
    private CvSeq largestContour = null;
    //  private CvSeq secondLargestContour = null;
    CvMemStorage storage = CvMemStorage.create();

    public int findLargeContours(IplImage image) {

        // Returns 2: If found 2 fingers
        // Returns 1: If found 1 finger
        // Returns 0: If nothing found or largest area is soo small

        largest = 0;
        //    secondLargest = 0;
        largestContour = new CvSeq(null);
        //     secondLargestContour = new CvSeq(null);

        CvSeq contours = new CvSeq(null);
        final int MIN_CONTOUR_AREA = 200;
        //  final int MAX_AREA_DIFF = 4000;

        int numCont = cvFindContours(image, storage, contours, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

        if (numCont == 0 || contours == null || contours.isNull()) {

            //  cvClearSeq(largestContour);
            return 0;
        } else if (numCont == 1) {

            largest = cvContourArea(contours, CV_WHOLE_SEQ, 0);
            // System.out.println("Area= "+largest);
            if (largest < MIN_CONTOUR_AREA) {
                //  cvClearSeq(largestContour);
                return 0;

            } else {
                largestContour = contours;
                return 1;
            }
        } else {
            while (contours != null && !contours.isNull()) {

                result1 = cvContourArea(contours, CV_WHOLE_SEQ, 0);

                if (result1 > largest) {
                    //     secondLargest = largest;
                    largest = result1;
                    largestContour = contours;
                }

//                if (secondLargest < result1 && largest != result1) {
//                    secondLargest = result1;
//                    secondLargestContour = contours;
//                }
                contours = contours.h_next();
            }

            if (largest < MIN_CONTOUR_AREA) {
                //   cvClearSeq(largestContour);
                return 0;

            } else {
                // double areaDiff = largest - secondLargest;
                //System.out.println("Area Difference= "+areaDiff);

//                if (areaDiff < MAX_AREA_DIFF) { // This should be defined well in order to capture 2 fingers
//                    return 2;
//
//                } else {
//                    secondLargestContour.setNull();
//                    return 1;
//                }
                return 1;
            }
        }
    }

    public CvSeq getLargestContour() {
        return largestContour;
    }
//    public CvSeq getSecondLargestContour() {
//        return secondLargestContour;
//    }
}
